const { ccclass, property } = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    private static IOS: string = "IOS";
    private static AND: string = "AND";

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Node)
    btnBack: cc.Node = null;

    @property(cc.Node)
    btnChange: cc.Node = null;

    @property
    text: string = 'Hello, Cocos!';

    private _isCocos: boolean;
    private _platform: string;

    protected onEnable(): void {
        this.btnBack.on(cc.Node.EventType.TOUCH_END, this.onBack, this);
        this.btnChange.on(cc.Node.EventType.TOUCH_END, this.onChange, this);
        this._platform = this.getPlatform();
        cc.warn("---------> 进入游戏");
        cc.warn("---------> platform=" + this._platform);
    }

    protected onDestroy(): void {
        this.btnBack.off(cc.Node.EventType.TOUCH_END, this.onBack, this);
        this.btnChange.off(cc.Node.EventType.TOUCH_END, this.onChange, this);
    }

    private onBack(): void {
        if (this._platform == "Ios") {
            jsb.reflection.callStaticMethod("OCBridge", "exitGame");
        }
        else if (this._platform == "Android") {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "exitGame", "()V");
        }
        cc.warn("---------> 退出游戏");
    }

    private onChange(): void {
        this._isCocos = !this._isCocos;
        this.label.string = this._isCocos ? this.text : "Hello, World!";
    }

    private getPlatform(): string {
        if (cc.sys.isNative) {
            if (cc.sys.os == cc.sys.OS_IOS) {
                return "Ios";
            }
            else if (cc.sys.os == cc.sys.OS_ANDROID) {
                return "Android";
            }
        }
        return "unknown";
    }
}
